package com.leoman.utils;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.apache.commons.lang.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import javax.servlet.http.HttpServletRequest;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.net.URISyntaxException;
import java.util.*;


/**
 * @author ChengYu
 */
public class CommonUtils {

    private static Properties properties = null;

    static {
        if (properties == null) {
            properties = new Properties();
        }
    }

    /**
     * json对象转换成map对象
     * @param result
     * 			响应结果
     * @return map对象
     * */
    public static HashMap<String, Object> jsonToMap(String result) {
        HashMap<String, Object> hashMap = new HashMap<String, Object>();
        JsonParser parser = new JsonParser();
        try
        {
            JsonObject asJsonObject = parser.parse(result).getAsJsonObject();
            Set<Map.Entry<String, JsonElement>> entrySet = asJsonObject.entrySet();
            for (Map.Entry<String, JsonElement> m : entrySet) {
                hashMap.put(m.getKey(), m.getValue());
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return hashMap;
    }
    /**
     * 将xml数据转换成map对象
     * @param xml
     * 			数据
     * @return Map 对象
     */
    public static HashMap<String, Object> xmlToMap(String xml) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        Document doc = null;
        try {
            doc = DocumentHelper.parseText(xml); // 将字符串转为XML
            Element rootElt = doc.getRootElement(); // 获取根节点
            for (Iterator i = rootElt.elementIterator(); i.hasNext();) {
                Element e = (Element) i.next();
                map.put(e.getName(), e.getText());
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }

    /**
     * 将对象转换成POJO类
     *
     * @param type
     * @param map
     * @return
     * @throws IntrospectionException
     * @throws IllegalAccessException
     * @throws InstantiationException
     * @throws InvocationTargetException
     */
    public static Object convertMapToPOJO(Class<?> type, Map<String, Object> map)
            throws IntrospectionException, IllegalAccessException, InstantiationException, InvocationTargetException {
        BeanInfo beanInfo = Introspector.getBeanInfo(type); // 获取类属性
        Object obj = type.newInstance(); // 创建 JavaBean 对象

        // 给 JavaBean 对象的属性赋值
        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
        for (int i = 0; i < propertyDescriptors.length; i++) {
            PropertyDescriptor descriptor = propertyDescriptors[i];
            String propertyName = descriptor.getName();
            if (map.containsKey(propertyName)) {
                Object value = map.get(propertyName);
                Object[] args = new Object[1];
                args[0] = value;
                descriptor.getWriteMethod().invoke(obj, args);
            }
        }
        return obj;
    }

    /**
     * 获取当前系统时间
     *
     * @return
     */
    public static Date getCurrentDate(String fomater) {
        return new Date();
    }

    /**
     * 手机号中间4位隐藏
     *
     * @author gaolei
     */
    public static String getBeHidedMobile(String mobile) {
        if (mobile.length() < 11) {
            return mobile;
        }
        String a = mobile.substring(0, 3);
        String b = mobile.substring(7, 11);
        return a + "****" + b;
    }

    // 根据url获取商品id
    // String t = "http://www.uhoem.com/product/detail/130.jhtml";
    public static String getProductid(String t) {
        if (StringUtils.isNotEmpty(t)) {
            return "";
        }
        t = t.substring(t.lastIndexOf("/") + 1);
        t = t.substring(0, t.lastIndexOf("."));
        return t;
    }

    public static String generateUUID() {
        UUID uuid = UUID.randomUUID();
        return uuid.toString().replaceAll("-", "");
    }

    public static String mapToXml(Map<String, Object> map) {

        StringBuffer buffer = new StringBuffer();
        buffer.append("<xml>");

        for (Map.Entry<String, Object> entity : map.entrySet()) {
            String key = entity.getKey();
            Object obj = entity.getValue();

            buffer.append("<").append(key).append(">").append(obj.toString()).append("</").append(key).append(">");
        }
        buffer.append("</xml>");
        return buffer.toString();
    }

    public static String getRealAddress(HttpServletRequest request) {

        String ip = request.getHeader("X-Forwarded-For");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("Proxy-Client-IP");
            }
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("WL-Proxy-Client-IP");
            }
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("HTTP_CLIENT_IP");
            }
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("HTTP_X_FORWARDED_FOR");
            }
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getRemoteAddr();
            }
        } else if (ip.length() > 15) {
            String[] ips = ip.split(",");
            for (int index = 0; index < ips.length; index++) {
                String strIp = (String) ips[index];
                if (!("unknown".equalsIgnoreCase(strIp))) {
                    ip = strIp;
                    break;
                }
            }
        }
        return ip;
    }

    /**
     * 生成验证码
     *
     * @param length
     * @return
     */
    public static String getCode(int length) {
        Random random = new Random();
        StringBuffer result = new StringBuffer();
        for (int i = 0; i < length; i++) {
            result.append(random.nextInt(10));
        }
        return result.toString();
    }

    public static void main(String[] args) {
//		Map<String, Object> map = new HashMap<String, Object>();
//		map.put("user", "yesong");
//		System.out.println(mapToXml(map));

        String reg = "^[0-9]+(,[0-9]+)*$";
        openUrl("http://localhost:8080/yqss/moneymag/index?userId=7");

    }


    /**
     * 生成订单号
     */
    public static String generateSn() {
        Random r = new Random();
        int random = r.nextInt(1000);
        long current = System.currentTimeMillis();
        return String.valueOf(current) + String.valueOf(random);
    }

    public static String getSessionId() {
        return null;
    }

    // 判断map里的参数是否有为空
    public static Boolean existEmpty(Map<String, Object> params, String... key) {
        for (int i = 0; i < key.length; i++) {
            if (StringUtils.isEmpty((String) params.get(key[i]))) {
                return true;
            }
        }
        return false;
    }

    public static void openUrl(String url) {
        java.net.URI uri;
        try {
            uri = new java.net.URI(url);
            java.awt.Desktop.getDesktop().browse(uri);
        } catch (URISyntaxException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}